package com.ruiyian.system.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruiyian.common.vo.TreeSelect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruiyian.common.core.domain.entity.SysDictData;
import com.ruiyian.common.utils.DictUtils;
import com.ruiyian.system.mapper.SysDictDataMapper;
import com.ruiyian.system.service.ISysDictDataService;

/**
 * 字典 业务层处理
 *
 * @author ruiyian
 */
@Service
public class SysDictDataServiceImpl implements ISysDictDataService
{
    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 根据条件分页查询字典数据
     *
     * @param dictData 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataList(SysDictData dictData)
    {
        return dictDataMapper.selectDictDataList(dictData);
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType 字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue)
    {
        return dictDataMapper.selectDictLabel(dictType, dictValue);
    }

    /**
     * 根据字典数据ID查询信息
     *
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public SysDictData selectDictDataById(Long dictCode)
    {
        return dictDataMapper.selectDictDataById(dictCode);
    }

    /**
     * 批量删除字典数据信息
     *
     * @param dictCodes 需要删除的字典数据ID
     * @return 结果
     */
    @Override
    public void deleteDictDataByIds(Long[] dictCodes)
    {
        for (Long dictCode : dictCodes)
        {
            SysDictData data = selectDictDataById(dictCode);
            dictDataMapper.deleteDictDataById(dictCode);
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
    }

    /**
     * 新增保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(SysDictData data)
    {
        int row = dictDataMapper.insertDictData(data);
        if (row > 0)
        {
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
        return row;
    }

    /**
     * 修改保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(SysDictData data)
    {
        int row = dictDataMapper.updateDictData(data);
        if (row > 0)
        {
            List<SysDictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
        return row;
    }

    /**
     *  根据父级id获取列表
     * @param parentId
     * @return
     */
    @Override
    public List<SysDictData> selectListByParnetId(Long parentId) {
        return dictDataMapper.selectListByParnetId(parentId);
    }
    /**
     * 构建树结构
     * @param sysDictData
     * @return
     */
    @Override
    public List<TreeSelect> buildTreeSelect(List<SysDictData> sysDictData) {
        List<SysDictData> menuTrees = buildMenuTree(sysDictData);
        return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    public List<SysDictData> buildMenuTree(List<SysDictData> menus) {
        List<SysDictData> returnList = new ArrayList<>();
        for (Iterator<SysDictData> iterator = menus.iterator(); iterator.hasNext(); ) {
            SysDictData t = iterator.next();
            // 根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (t.getParentId() == 0) {
                recursionFn(menus, t);
                returnList.add(t);
            }
        }
        if (returnList.isEmpty()) {
            returnList = menus;
        }
        return returnList;
    }
    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<SysDictData> list, SysDictData t) {
        // 得到子节点列表
        List<SysDictData> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysDictData tChild : childList) {
            if (hasChild(list, tChild)) {
                // 判断是否有子节点
                Iterator<SysDictData> it = childList.iterator();
                while (it.hasNext()) {
                    SysDictData n = it.next();
                    recursionFn(list, n);
                }
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<SysDictData> getChildList(List<SysDictData> list, SysDictData t) {
        List<SysDictData> tlist = new ArrayList<>();
        Iterator<SysDictData> it = list.iterator();
        while (it.hasNext()) {
            SysDictData n = it.next();
            if (n.getParentId()!=null && n.getParentId().longValue() == t.getDictCode().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysDictData> list, SysDictData t) {
        return getChildList(list, t).size() > 0;
    }
    @Override
    public int checkName(SysDictData sysDictData) {
        return dictDataMapper.checkName(sysDictData);
    }
    /**
     * 删除
     * @param code
     */
    @Override
    public void deleteDictDataById(Long code) {
        dictDataMapper.deleteDictDataById(code);
    }


    @Override
    public List<SysDictData> selectListByTunnelId(String id) {
        return dictDataMapper.selectListByTunnelId(id);
    }
}
